﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
namespace DalMgr
{
    public class SftLog
    {
#region DLL 声明
        /// <summary>
        /// SendMessage
        /// </summary>
        /// <param name="hWnd">消息发送的窗体句柄</param>
        /// <param name="Msg">消息值</param>
        /// <param name="wParam">参数1</param>
        /// <param name="lParam">参数2</param>
        /// <returns></returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
        /// <summary>
        /// SendMessage
        /// </summary>
        /// <param name="hWnd">消息发送的窗体句柄</param>
        /// <param name="Msg">消息值</param>
        /// <param name="wParam">参数1</param>
        /// <param name="lParam">进度条结构体</param>
        /// <returns></returns>
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, ref  MsgStruct lParam);

#endregion

        /// <summary>
        /// 日志文件夹名称
        /// </summary>
        private const string LOG_DIR = "Log\\";

        /// <summary>
        /// 消息类
        /// </summary>
        private static SftLog sftLog = null;

        /// <summary>
        /// 日志文件路径
        /// </summary>
        private static string LogFilePath;

        /// <summary>
        /// 消息发送的句柄
        /// </summary>
        private IntPtr HwndSenTo = IntPtr.Zero;

        private SftLog()
        {
            string LogDir = AppDomain.CurrentDomain.BaseDirectory + LOG_DIR;
            if (!Directory.Exists(LogDir))
            {
                Directory.CreateDirectory(LogDir);
            }
            LogFilePath = AppDomain.CurrentDomain.BaseDirectory + LOG_DIR + DateTime.Now.ToString("yyyy-MM-dd")+ "-Log.log";
        }

        public static SftLog GetSftLog()
        {
            if (sftLog == null)
            {
                sftLog = new SftLog();
            }
            return sftLog;
        }
        /// <summary>
        /// 设置消息发送到的窗体句柄
        /// </summary>
        /// <param name="hwnd">窗体句柄</param>
        public void SetLogReceiveHwnd(IntPtr hwnd)
        {
            HwndSenTo = hwnd;
        }
        /// <summary>
        /// 写入日志
        /// 主界面显示和写入到文件
        /// </summary>
        /// <param name="str"></param>
        public void WriteLog(string str)
        {
            string strMsg = DateTime.Now.ToString("HH:mm:ss") + "  " + str;
            WriteLogToFile(strMsg);
            MsgStruct msgSt = new MsgStruct();
            msgSt.Status = strMsg;
            SendMessage(HwndSenTo, (int)WM_MSG.WM_MSG_LOG, IntPtr.Zero, ref msgSt);
        }
        /// <summary>
        /// 发送升级消息到主窗体
        /// </summary>
        public void HasUpdate()
        {
            MsgStruct msgSt = new MsgStruct();
            SendMessage(HwndSenTo, (int)WM_MSG.WM_MSG_UPDATE, IntPtr.Zero, ref msgSt);
        }
        /// <summary>
        /// 将日志写入到文件
        /// </summary>
        /// <param name="_Log">日志字符串</param>
        public void WriteLogToFile(string _Log)
        {
            using (FileStream FS = new FileStream(LogFilePath, FileMode.Append, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(FS))
                {
                    sw.WriteLine(_Log);
                    sw.Close();
                    FS.Close();
                }
            }
        }
    }
}
